package com.hwd.gmall.product.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwd.gmall.model.product.SkuInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 库存单元表的 mapper 映射
 *
 * @author 黄伟东/Victor
 * @date 2022/4/20 19:27
 */
@Mapper
public interface SkuInfoMapper extends BaseMapper<SkuInfo> {

    /**
     * 扣减库存
     *
     * @param skuId  商品id
     * @param skuNum 扣减数量
     * @return int 扣减成功数量
     */
    @Update("UPDATE sku_info SET stock = stock - #{skuNum} WHERE id = #{skuId} AND stock >= #{skuNum}")
    int deductStock(@Param("skuId") Long skuId, @Param("skuNum") Integer skuNum);

    /**
     * 回滚库存
     *
     * @param skuId  商品id
     * @param skuNum 回滚数量
     * @return int 回滚库存成功数量
     */
    @Update("UPDATE sku_info SET stock = stock + #{skuNum} WHERE id = #{skuId}")
    int rollBackStock(@Param("skuId") Long skuId, @Param("skuNum") Integer skuNum);
}
